<template>
	<view class="price-setting">
		<view class="page-title">基础价格</view>
		<view class="header-box">
			<view class="header-item">
				<view>包间价格</view>
				<view class="header-item-flex">
					<uni-easyinput class="pop-input" type="number" :maxlength="10" primaryColor="#ffd589" trim="all"
						v-model="priceInfo.price" placeholder="请输入包间价格"></uni-easyinput>
					<text style="margin-left: 10rpx;">元/小时</text>
				</view>
			</view>
			<view class="header-item">
				<view>包间原价</view>
				<view class="header-item-flex">
					<uni-easyinput class="pop-input" type="number" :maxlength="10" primaryColor="#ffd589" trim="all"
						v-model="priceInfo.oriPrice" placeholder="请输入包间原价"></uni-easyinput>
					<text style="margin-left: 10rpx;">元/小时</text>
				</view>
			</view>
			<view class="save-btn" @click="savePrice">保 存</view>
		</view>
		<view class="page-title">时段价格</view>
		<view class="card-box">
			<view class="card" v-for="(item,index) in priceList" :key="index">
				<view class="card-item card-item-title">名称：{{item.timePeriodName}}</view>
				<view class="card-item">类型：{{item.type === 1?'自然周':'节假日'}}</view>
				<view class="card-item">价格：{{item.price}}元</view>
				<view class="card-item">
					<text>日期范围：</text>
					<text v-for="(date,id) in item.weekArr" :key="id">{{weekObj[date]}}
						<text v-if="id<item.weekArr.length-1">、</text> </text>
				</view>
				<view class="card-item">时间范围：{{item.startTime}} ~ {{item.endTime}}</view>
				<view class="flex-box">
					<view class="card-item"></view>
					<view class="btn-box">
						<view class="edit-btn" @click="openPop('edit',item)">编辑</view>
						<view class="delete-btn" @click="deleteEvent(item)">删除</view>
					</view>
				</view>
			</view>
		</view>
		<view class="noMore">暂无更多</view>
		<view class="footer-box">
			<text class="add-btn" @click="openPop('add')">添加时段价格</text>
		</view>
		<uni-popup ref="popup" type="bottom" :safe-area='false'>
			<view class="pop-box">
				<view>
					<view class="pop-title">{{type==='add'?'新增时段价格':'编辑时段价格'}}</view>
					<view class="pop-item">
						<view class="pop-item-title">类型：</view>
						<uni-data-select v-model="typeRangeVal" :clear="false" :localdata="typeRange"
							@change="changeType"></uni-data-select>
					</view>
					<view class="pop-item">
						<view class="pop-item-title">时段名称：</view>
						<uni-data-select v-model="timeRangeVal" :clear="false" :localdata="timeRange"
							@change="changeTime"></uni-data-select>
					</view>
					<view v-show="currentTime.timePeriodId">
						<view class="pop-text">
							<text>日期范围：</text>
							<text v-for="(date,id) in currentTime.weekArr" :key="id">{{weekObj[date]}} <text
									v-if="id<currentTime.weekArr.length-1">、</text> </text>
						</view>
						<view class="pop-text">时间范围：{{currentTime.startTime}}~{{currentTime.endTime}}</view>
					</view>
					<view class="pop-item">
						<view class="pop-item-title">价格：</view>
						<uni-easyinput class="pop-input" type="number" :maxlength="10" primaryColor="#ffd589" trim="all"
							v-model="currentConfig.price" placeholder="请输入价格"></uni-easyinput>元/小时
					</view>
				</view>
				<view class="pop-btn-box">
					<view class="cancel-btn" @click="closePop">取消</view>
					<view class="submit-btn" @click="submitEvent">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shopId: '',
				priceList: [],
				currentConfig: {
					timePeriodId: '',
					price: '',
					type: '',
					weeks: '',
					startDate: '',
					endDate: '',
					startTime: '',
					endTime: ''
				},
				priceInfo: {
					price: '',
					oriPrice: ''
				},
				type: '',
				weekObj: {
					sun: '周日',
					mon: '周一',
					tue: '周二',
					wed: '周三',
					thu: '周四',
					fri: '周五',
					sat: '周六',
				},
				typeRangeVal: 1,
				typeRange: [{
					value: 1,
					text: '自然周'
				}],
				timeRangeVal: '',
				timeRange: [],
				currentTime: {}
			};
		},
		methods: {
			// 选择类型
			changeType(e) {
				this.currentConfig.type = e
			},
			// 选择时段
			changeTime(e) {
				this.currentConfig.timePeriodId = e
				this.timeRange.forEach((item) => {
					if (item.timePeriodId === this.currentConfig.timePeriodId) {
						this.currentTime = item
					}
				})
			},
			// 重置数据
			resetData() {
				this.currentConfig = {
					timePeriodId: '',
					price: '',
					type: '',
					weeks: '',
					startDate: '',
					endDate: '',
					startTime: '',
					endTime: ''
				}
				this.currentTime = {}
				this.timeRangeVal = ''
			},
			// 打开弹窗
			openPop(type, item) {
				this.type = type
				// 通过组件定义的ref调用uni-popup方法 ,如果传入参数 ，type 属性将失效 ，仅支持 ['top','left','bottom','right','center']
				if (type === 'edit') {
					this.currentConfig = JSON.parse(JSON.stringify(item));
					this.timeRangeVal = this.currentConfig.timePeriodId
					this.timeRange.forEach((item) => {
						if (item.timePeriodId === this.currentConfig.timePeriodId) {
							this.currentTime = item
						}
					})
				} else {
					this.resetData()
				}
				this.$refs.popup.open('center')
			},
			closePop() {
				this.$refs.popup.close()
			},
			// 添加/编辑时段价格
			submitEvent() {
				if (!this.currentTime.timePeriodId) {
					uni.showToast({
						title: '请选择时段',
						duration: 2000,
						icon: 'none'
					})
					return
				} else if (!this.currentConfig.price) {
					uni.showToast({
						title: '请输入价格',
						duration: 2000,
						icon: 'none'
					})
					return
				}
				const data = {
					...this.currentConfig,
					...this.currentTime,
					productId: this.productId,
					type: this.typeRangeVal,
				}
				let method = ''
				if (this.type === 'add') {
					method = 'post'
				} else {
					method = "put"
				}
				//保存设备
				this.$request({
					method: method,
					url: '/applet/store/productPriceConfig',
					data,
					loading: true,
					isEncrypt: false
				}).then(res => {
					if (res.code === 200) {
						this.getpriceList()
						uni.showToast({
							title: '添加成功',
							duration: 2000,
							icon: 'none'
						})
						this.closePop()
						this.resetData()
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			},

			// 删除账号
			deleteEvent(item) {
				uni.showModal({
					title: '提示',
					content: '是否确认删除该时段价格？',
					cancelText: '取消',
					cancelColor: '#666',
					confirmText: '确认',
					confirmColor: '##ff9600',
					success: res => {
						if (res.confirm) {
							const data = {
								productId: this.productId
							}
							this.$request({
								method: 'DELETE',
								url: `/applet/store/productPriceConfig/${item.priceConfigId}`,
								data,
								loading: false,
								isEncrypt: false
							}).then(res => {
								if (res.code === 200) {
									this.getpriceList()
									uni.showToast({
										title: '删除成功',
										duration: 2000,
										icon: 'none'
									})
								} else {
									uni.showToast({
										title: res.msg,
										duration: 2000,
										icon: 'none'
									})
								}
							}).catch(err => {
								console.error('接口异常：', err)
							})
						}
					}
				})
			},
			// 保存包间基础价格
			savePrice() {
				if (!this.priceInfo.price) {
					uni.showToast({
						title: '请输入包间价格',
						duration: 2000,
						icon: 'none'
					})
					return
				} else if (!this.priceInfo.oriPrice) {
					uni.showToast({
						title: '请输入包间原价',
						duration: 2000,
						icon: 'none'
					})
					return
				}
				const data = {
					productId: this.productId,
					price: this.priceInfo.price,
					oriPrice: this.priceInfo.oriPrice,
					priceConfigId: this.priceInfo.priceConfigId,
					type: 0
				}
				this.$request({
					method: 'put',
					url: `/applet/store/productPriceConfig`,
					data,
					loading: false,
					isEncrypt: false
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '保存成功',
							duration: 2000,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						})
					}
				}).catch(err => {
					console.error('查询异常：', err)
				})
			},
			//获取周时段价格列表
			getpriceList() {
				const data = {
					productId: this.productId,
					type: 1
				}
				this.$request({
					method: 'post',
					url: `/applet/store/productPriceConfig/list`,
					data,
					loading: false,
					isEncrypt: false
				}).then(res => {
					this.priceList = res.rows || []
					this.priceList.forEach((item) => {
						item.weekArr = item.weeks.split(',')
					})
				}).catch(err => {
					console.error('查询异常：', err)
				})
			},
			// 获取基础价格
			getBasePrice() {
				this.$request({
					method: 'get',
					url: `/applet/store/productPriceConfig/getBasePrice/${this.productId}`,
					loading: false,
					isEncrypt: false
				}).then(res => {
					const {
						price,
						oriPrice,
						priceConfigId
					} = res.data
					this.priceInfo.price = price
					this.priceInfo.oriPrice = oriPrice
					this.priceInfo.priceConfigId = priceConfigId
				}).catch(err => {
					console.error('查询异常：', err)
				})
			},
			//获取时段列表
			getTimeList() {
				const data = {
					shopId: this.shopId
				}
				this.$request({
					method: 'post',
					url: `/applet/store/productTimePeriod/list`,
					data,
					loading: false,
					isEncrypt: false
				}).then(res => {
					res.rows.forEach((item) => {
						item.weekArr = item.weeks.split(',')
						item.text = item.timePeriodName
						item.value = item.timePeriodId
					})
					this.timeRange = res.rows
				}).catch(err => {
					console.error('查询异常：', err)
				})
			}
		},
		onLoad(options) {
			this.productId = options.productId
			this.shopId = options.shopId
			this.getBasePrice()
			this.getpriceList()
			this.getTimeList()
		}
	}
</script>

<style lang="scss" scoped>
	.price-setting {
		padding-bottom: 112rpx;
		background: #FFF;

		::v-deep .uni-easyinput {
			width: 240rpx;
		}

		.page-title {
			position: relative;
			padding: 30rpx 30rpx 0 50rpx;
			font-weight: bold;
		}

		.page-title::before {
			content: '';
			width: 8rpx;
			height: 34rpx;
			position: absolute;
			left: 30rpx;
			top: 36rpx;
			background: #ff9600;
		}

		.header-box {
			padding: 0 30rpx;
			background: #FFF;

			.header-item {
				border-bottom: 1px solid #EEE;
				padding: 20rpx 0;
				display: flex;
				align-items: center;
				justify-content: space-between;
			}

			.header-item-flex {
				display: flex;
				align-items: center;
			}
		}

		.card-box {
			padding: 30rpx;
		}

		.card {
			background: #FFF;
			border-radius: 10rpx;
			padding: 20rpx;
			box-shadow: 0 2px 6px #CCC;
			margin-bottom: 20rpx;
		}

		.card-item {
			color: #333;
			line-height: 64rpx;
			font-size: 26rpx;
		}

		.card-item-title {
			font-weight: bold;
			color: #000;
			font-size: 28rpx;
		}

		.flex-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.btn-box {
			display: flex;
			align-items: center;
			font-size: 28rpx;

			.edit-btn {
				padding: 8rpx 16rpx;
				font-size: 24rpx;
				border-radius: 10rpx;
				border: 1px solid #ff9600;
			}

			.delete-btn {
				padding: 8rpx 16rpx;
				font-size: 24rpx;
				border-radius: 10rpx;
				color: #FFF;
				background: #ff9600;
				margin-left: 20rpx;
			}
		}

		.footer-box {
			width: 100%;
			position: fixed;
			bottom: 0;
			padding: 30rpx 0;
			text-align: center;
			background: #FFF;

			.add-btn {
				display: inline-block;
				color: #FFF;
				background: #ff9600;
				font-size: 24rpx;
				border-radius: 10rpx;
				padding: 10rpx 60rpx;
			}
		}

		.pop-box {
			background: #FFF;
			border-radius: 20rpx;
			padding: 30rpx;
			width: 600rpx;

			.pop-title {
				font-size: 32rpx;
				font-weight: 600;
				text-align: center;
				margin-bottom: 30rpx;
			}

			.pop-item-title {
				width: 140rpx;
				text-align: right;
			}

			.pop-item {
				display: flex;
				align-items: center;
				font-size: 26rpx;
				margin-bottom: 30rpx;
			}

			.pop-text {
				font-size: 26rpx;
				margin-bottom: 30rpx;
			}

			.pop-unit {
				margin-left: 10rpx;
			}

			.pop-btn-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 60rpx;

				.cancel-btn {
					padding: 8rpx 36rpx;
					font-size: 24rpx;
					border-radius: 10rpx;
					border: 1px solid #CCC;
				}

				.submit-btn {
					padding: 8rpx 36rpx;
					font-size: 24rpx;
					border-radius: 10rpx;
					color: #FFF;
					background: #ff9600;
				}
			}
		}

		.noMore {
			text-align: center;
			padding: 32rpx 0;
			color: #CCC;
			font-size: 28rpx;
		}

		.back-btn {
			padding-left: 16rpx;
		}

		.save-btn {
			padding: 10rpx 0;
			border-radius: 10rpx;
			background: #ff9600;
			color: #FFF;
			width: 200rpx;
			box-sizing: border-box;
			text-align: center;
			margin: 20rpx auto;
		}
	}
</style>